package cn.kk.elasticsearch;

import java.net.InetAddress;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class EsTest {

	/*
	 * 客户端连接es,执行一个方法判断连接是否成功
	 */
	public static TransportClient getClient() throws Exception {
		// 设置集群名称
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
     // 创建client
        TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.64.128"), 9300));
		/* client.addTransportAddress(transportAddress) */
		// 如果是多个节点,在add方法执行后addTransportAddress
		// client继续调用另外一个add,
		return client;
	}

	@Test
	public void test01() throws Exception {
		// java TransportClient client 连接es
		// Settings.Empty表示连接过程使用默认配置
		// 例如集群名称默认是elasticsearch
		TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.64.128"), 9300));
		// 利用client的方法,获取索引jtdb,tb_item 下id为830972的doc
		GetResponse response = client.prepareGet("jtdb", "tb_item", "830972").get();
		// 从response,获取doc数据
		String result = response.getSourceAsString();
		System.out.println(result);
	}

	/*
	 * 判断索引是否存在
	 */
	@Test
	public void test02() throws Exception {
		TransportClient client = EsTest.getClient();
		// 操作索引判断索引,需要从连接对象中获取索引管理对象
		IndicesAdminClient indexClient = client.admin().indices();
		// 索引管理对象调用exists方法,获取响应结果
		IndicesExistsResponse response01 = indexClient.prepareExists("index1").get();
		IndicesExistsResponse response02 = indexClient.prepareExists("jtdb").get();
		System.out.println(response01.isExists());
		System.out.println(response02.isExists());
	}

	/*
	 * 创建索引
	 */
	@Test
	public void test03() throws Exception {
		TransportClient client = EsTest.getClient();
		// 获取管理对象
		IndicesAdminClient indexClient = client.admin().indices();
		CreateIndexResponse response = indexClient.prepareCreate("index03").get();
		System.out.println(response.isAcknowledged());
	}

	/*
	 * 删除索引
	 */
	@Test
	public void test04() throws Exception {
		TransportClient client = EsTest.getClient();
		IndicesAdminClient indexClient = client.admin().indices();
		indexClient.prepareDelete("index03").get();
	}
	/*
	 * 新建文档 文档是对象,底层http协议如何将对象转化成json字符串 引入一个第三方工具jacksonJson:将对象转化成json字符串,
	 * 将json字符串转化成对象;
	 */
	/*
	 * 测试jackson包转化对象称为json字符串
	 */
	
	  /*private static final ObjectMapper mapper= new ObjectMapper();
	  
	  @Test public void test05() throws Exception{ Item item=new Item();
	  item.setId(10l); item.setBarcode("sdfas"); item.setTitle("三星大地雷");
	  item.setSellPoint("灭口好武器"); String docStr=new ObjectMapper().
	  writeValueAsString(item); //client调用添加方法将字符串通过http发送到es存储 IndexResponse
	  response = EsTest.getClient(). prepareIndex("index01", "item", "1").
	  setSource(docStr).execute().actionGet();
	  System.out.println(response.status()); }
	 */

	/*
	 * matchQuery 查询数据
	 */
	@Test
	public void test06() throws Exception {
		// 连接对象
		TransportClient client = EsTest.getClient();
		// 构造查询条件对象 name field名称 text查询关键字
		// org.elasticsearch.index.query
		QueryBuilder query = QueryBuilders.rangeQuery("price").lt(10000);
		// 获取返回结果,打印部分字段
		// search指向一个索引文件名
		SearchResponse response = client.prepareSearch("jtdb").setQuery(query).setSize(10).get();
		// response里包含着50条查询结果
		SearchHits hits = response.getHits();// 查询条数
		for (SearchHit hit : hits) {
			System.out.println("文档ID:" + hit.getId());
			System.out.println("商品price:" + hit.getSourceAsMap().get("price"));
			System.out.println("标题:" + hit.getSourceAsMap().get("title"));
		}
	}
}
